package com.imodia.checkoo.webtag;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hsq.commons.util.beans.BeanUtils;
import com.imodia.checkoo.bean.BaseArticleBean;
import com.imodia.checkoo.bean.BasePageBean;
import com.imodia.checkoo.bean.CmsArticleBean;
import com.imodia.checkoo.bean.CmsOfferJobBean;
import com.imodia.checkoo.bean.CmsPageAdBean;
import com.imodia.checkoo.bean.CmsPageBean;
import com.imodia.checkoo.lucence.LucenceDataBean;


public final class TldUtil {
	
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(TldUtil.class);
	

	public final static StringBuffer convertPage(List<CmsPageBean> list,TldParam param){
		StringBuffer buf = new StringBuffer();
		String[] fieldArr = param.getFields().split(",");
		for(int i=0;i<list.size();i++){
			   BasePageBean obj = list.get(i);
			   String s = param.getHtmls();
			   for(int k = 0 ;k<fieldArr.length;k++){
				   s = s.replaceAll("\\{"+k+"\\}", String.valueOf(BeanUtils.get(obj, fieldArr[k])));
			   }
			   //log.error("param.getIsHtml():"+param.getIsHtml());
			   obj.setIsHtml(param.getIsHtml());
			   s = s.replaceAll("\\{a\\}", obj.getHrefUrl());
			   buf.append(s);
		}
		return buf;
	}
	
	public final static StringBuffer convertArtilce(List<CmsArticleBean> list,TldParam param){
		StringBuffer buf = new StringBuffer();
		String[] fieldArr = param.getFields().split(",");
		for(int i=0;i<list.size();i++){
			   BaseArticleBean obj = list.get(i);
			   String s = param.getHtmls();
			   for(int k = 0 ;k<fieldArr.length;k++){
				   s = s.replaceAll("\\{"+k+"\\}", String.valueOf(BeanUtils.get(obj, fieldArr[k])));
			   }
			   //log.error("param.getIsHtml():"+param.getIsHtml());
			   obj.setIsHtml(param.getIsHtml());
			   obj.setPageId(param.getRedirectPageId());
			   s = s.replaceAll("\\{a\\}", obj.getHrefUrl());
			   buf.append(s);
		}
		return buf;
	}
	
	public final static StringBuffer convertJob(List<CmsOfferJobBean> list,TldParam param){
		StringBuffer buf = new StringBuffer();
		String[] fieldArr = param.getFields().split(",");
		for(int i=0;i<list.size();i++){
				CmsOfferJobBean obj = list.get(i);
			   String s = param.getHtmls();
			   for(int k = 0 ;k<fieldArr.length;k++){
				   s = s.replaceAll("\\{"+k+"\\}", String.valueOf(BeanUtils.get(obj, fieldArr[k])));
			   }
			   //log.error("param.getIsHtml():"+param.getIsHtml());
//			   obj.setIsHtml(param.getIsHtml());
//			   s = s.replaceAll("\\{a\\}", obj.getHrefUrl());
			   buf.append(s);
		}
		return buf;
	}

	public final static StringBuffer convertAd(List<CmsPageAdBean> list,TldParam param){
		StringBuffer buf = new StringBuffer();
		String[] fieldArr = param.getFields().split(",");
		for(int i=0;i<list.size();i++){
				CmsPageAdBean obj = list.get(i);
				String s = param.getHtmls();
				for(int k = 0 ;k<fieldArr.length;k++){
					s = s.replaceAll("\\{"+k+"\\}", String.valueOf(BeanUtils.get(obj, fieldArr[k])));
				}
				buf.append(s);
		}
		return buf;
	}
	
	public final static StringBuffer convertLucence(List<LucenceDataBean> list,TldParam param){
		StringBuffer buf = new StringBuffer();
		String[] fieldArr = param.getFields().split(",");
		for(int i=0;i<list.size();i++){
			LucenceDataBean obj = list.get(i);
			   String s = param.getHtmls();
			   for(int k = 0 ;k<fieldArr.length;k++){
				   s = s.replaceAll("\\{"+k+"\\}", String.valueOf(BeanUtils.get(obj, fieldArr[k])));
			   }
			   s = s.replaceAll("\\{a\\}", obj.getHrefUrl());
			   buf.append(s);
		}
		return buf;
	}
	
}
